﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace visu.Controllers
{
    public class CompanyController : Controller
    {
        //
        // GET: /Company/
        #region======================================代理商======================================================
        public ActionResult CompanyRelationListInfo()
        {
            return View();
        }
        public ActionResult CompanyRelationDetail(int page, int rows, company company, string SearchContains, string sort, string order)
        {
            sort = string.IsNullOrEmpty(sort) ? "COMPANY_ID" : sort;
            order = string.IsNullOrEmpty(order) ? "asc" : order;

            SearchCondition condition = new SearchCondition() { Field = "COMPANY_TYPE", Operator = "=", Value = "1" };
            List<SearchCondition> listSearch = new List<SearchCondition>();
            listSearch.Add(condition);

            Expression<Func<company, bool>> exp = SearchExpression.GetSearchExpression<company>(company, listSearch, SearchContains, "COMPANY_FULLNAME", "COMPANY_NAME", "COPANNY_CHARGE", "COMPANY_PHONE", "COMPANY_CTIY");

            var list = BLLSession.Current.companyBLL.GetPagedList(page, rows, exp, sort, order);
            int count = DALSession.Current.companyDAL.GetCount(exp);
            LayuiDataPager dataPager = new LayuiDataPager() { code = 0, count = count, data = list, msg = "", total = count, rows = list };

            return Json(dataPager, JsonRequestBehavior.AllowGet);
        }
        #endregion


        #region======================================签约客户======================================================
        public ActionResult CompanyListInfo()
        {
            return View();
        }
        public ActionResult CompanyDetail(int page, int rows, company company, string SearchContains, string sort, string order)
        {
            sort = string.IsNullOrEmpty(sort) ? "COMPANY_ID" : sort;
            order = string.IsNullOrEmpty(order) ? "asc" : order;

            //SearchCondition condition = new SearchCondition() { Field = "ADD_COMPANY_ID", Operator = "=", Value = OperateContext.userInfo.COMPANY_ID.ToString() };
            List<SearchCondition> listSearch = new List<SearchCondition>();
            SearchCondition condition1 = new SearchCondition() { Field = "COMPANY_TYPE", Operator = "=", Value = "0" };
            listSearch.Add(condition1);

            Expression<Func<company, bool>> exp = SearchExpression.GetSearchExpression<company>(company, listSearch, SearchContains, "COMPANY_FULLNAME", "COMPANY_NAME", "COPANNY_CHARGE", "COMPANY_PHONE", "COMPANY_CTIY");

            var list = BLLSession.Current.companyBLL.GetPagedList(page, rows, exp, sort, order);
            int count = DALSession.Current.companyDAL.GetCount(exp);
            LayuiDataPager dataPager = new LayuiDataPager() { code = 0, count = count, data = list, msg = "", total = count, rows = list };

            return Json(dataPager, JsonRequestBehavior.AllowGet);
        }

        public ActionResult CompanyEdit()
        {
            string action = Request.QueryString["action"].ToString();
            int Id = Commons.String2Int32(Request.QueryString["Id"]);
            string width = Request.QueryString["width"].ToString();
            company info = new company();
            if (action == "add")
            {
                Id = 0; 
            }
            else
            {
                info = BLLSession.Current.companyBLL.GetListBy(c => c.COMPANY_ID == Id).FirstOrDefault();
            }

            ViewData.Model = info;
            ViewData["action"] = action;
            ViewData["Id"] = Id.ToString();
            ViewData["width"] = width;
            return View();
        }

        public ActionResult GetInfoById(int Id)
        {
            var company = BLLSession.Current.companyBLL.GetListBy(c => c.COMPANY_ID == Id).FirstOrDefault();
            return Json(company, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Add(company info)
        {
            info.CREATED_BY = OperateContext.userInfo.NAME;
            info.CREATED_TIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            info.ADD_COMPANY_ID = OperateContext.userInfo.COMPANY_ID;
            int companyId = Convert.ToInt16(BLLSession.Current.companyBLL.AddModelReturnId(info, "COMPANY_ID"));
            //初始化操作
            AddCompanyOperation(companyId);
            return Json(new Message() { state = "success" });
        }

        protected void AddCompanyOperation(int CompanyId)
        {
            SqlConnection sqlConn = new SqlConnection();
            sqlConn.ConnectionString = DBCallCommon.GetStringValue("connectionStrings");
            DBCallCommon.openConn(sqlConn);
            SqlCommand sqlCmd = new SqlCommand("PRO_AddCompanyOperation", sqlConn);
            sqlCmd.CommandTimeout = 300;
            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCmd.Parameters.Add("@CompanyId", SqlDbType.Int, 4);
            sqlCmd.Parameters["@CompanyId"].Value = CompanyId;
            sqlCmd.ExecuteNonQuery();
            DBCallCommon.closeConn(sqlConn);
        }

        public ActionResult Edit(company info)
        {
            info.CREATED_BY = OperateContext.userInfo.NAME;
            info.CREATED_TIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            BLLSession.Current.companyBLL.Modify(info, "COMPANY_FULLNAME","COMPANY_NAME","COPANNY_CHARGE","COMPANY_PHONE","COMPANY_CTIY","COMPANY_TYPE","CREATED_TIME","CREATED_BY");
            return Json(new Message() { state = "success" });
        }

        public ActionResult DeleteInfoById(int Id)
        {
            BLLSession.Current.companyBLL.DelBy((d => d.COMPANY_ID == Id));
            Message msg = new Message() { state = "success" };
            return Json(msg, JsonRequestBehavior.AllowGet);
        }

        #endregion

        #region=================================智能单元==================================================
        public ActionResult CabinetListInfo()
        {
            return View();
        }
        public ActionResult CabinetDetail(int page, int rows, vcabinet vcabinet, string SearchContains, string sort, string order)
        {
            sort = string.IsNullOrEmpty(sort) ? "COMPANY_ID" : sort;
            order = string.IsNullOrEmpty(order) ? "asc" : order;

            //SearchCondition condition = new SearchCondition() { Field = "COMPANY_ID", Operator = "=", Value = OperateContext.userInfo.COMPANY_ID.ToString() };
            List<SearchCondition> listSearch = new List<SearchCondition>();
            //listSearch.Add(condition);

            Expression<Func<vcabinet, bool>> exp = SearchExpression.GetSearchExpression<vcabinet>(vcabinet, listSearch, SearchContains, "CAB_NAME", "COMPANY_NAME",  "SUBPRODUCT_NO", "PRODUCT_NO");

            var list = BLLSession.Current.vcabinetBLL.GetPagedList(page, rows, exp, sort, order);
            int count = DALSession.Current.vcabinetDAL.GetCount(exp);
            LayuiDataPager dataPager = new LayuiDataPager() { code = 0, count = count, data = list, msg = "", total = count, rows = list };

            return Json(dataPager, JsonRequestBehavior.AllowGet);
        }

        public ActionResult CabinetEdit()
        {
            string action = Request.QueryString["action"].ToString();
            int Id = Commons.String2Int32(Request.QueryString["Id"]);
            string width = Request.QueryString["width"].ToString();
            cabinet info = new cabinet();
            if (action == "add")
            {
                Id = 0;
            }
            else
            {
                info = BLLSession.Current.cabinetBLL.GetListBy(c => c.CAB_ID == Id).FirstOrDefault();
            }
            var subproductlist = BLLSession.Current.vsubproductBLL.GetListBy(c => 1 == 1).ToList();
            ViewBag.subproductlist = subproductlist;

            ViewData.Model = info;
            ViewData["action"] = action;
            ViewData["Id"] = Id.ToString();
            ViewData["width"] = width;
            return View();
        }

        public ActionResult GetCabinetInfoById(int Id)
        {
            var cabinet = BLLSession.Current.cabinetBLL.GetListBy(c => c.CAB_ID == Id).FirstOrDefault();
            return Json(cabinet, JsonRequestBehavior.AllowGet);
        }
        public ActionResult AddCabinet(cabinet info)
        {
            var subproduct = BLLSession.Current.subproductBLL.GetListBy(c => c.SUB_PRODUCT_ID == info.SUB_PRODUCT_ID).FirstOrDefault();
            info.PRODUCT_ID = subproduct.PRODUCT_ID;
            info.COMPANY_ID = subproduct.COMPANY_ID;

            info.CREATED_BY = OperateContext.userInfo.NAME;
            info.CREATED_TIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            BLLSession.Current.cabinetBLL.Add(info);
            return Json(new Message() { state = "success" });
        }

        public ActionResult EditCabinet(cabinet info)
        {
            var subproduct = BLLSession.Current.subproductBLL.GetListBy(c => c.SUB_PRODUCT_ID == info.SUB_PRODUCT_ID).FirstOrDefault();
            info.PRODUCT_ID = subproduct.PRODUCT_ID;
            info.COMPANY_ID = subproduct.COMPANY_ID;

            info.CREATED_BY = OperateContext.userInfo.NAME;
            info.CREATED_TIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            BLLSession.Current.cabinetBLL.Modify(info, "PRODUCT_ID", "SUB_PRODUCT_ID", "CAB_NAME", "CAB_CODE", "CAB_STATUS", "CAB_TYPE","COMPANY_ID", "CREATED_TIME", "CREATED_BY");
            return Json(new Message() { state = "success" });
        }

        public ActionResult DeleteCabinetInfoById(int Id)
        {
            BLLSession.Current.cabinetBLL.DelBy((d => d.CAB_ID == Id));
            Message msg = new Message() { state = "success" };
            return Json(msg, JsonRequestBehavior.AllowGet);
        }


        #endregion

        #region=================================客户产品==================================================
        public ActionResult ProductListInfo()
        {
            return View();
        }
        public ActionResult ProductDetail(int page, int rows, vproduct vproduct, string SearchContains, string sort, string order)
        {
            sort = string.IsNullOrEmpty(sort) ? "COMPANY_ID" : sort;
            order = string.IsNullOrEmpty(order) ? "asc" : order;

            //SearchCondition condition = new SearchCondition() { Field = "COMPANY_ID", Operator = "=", Value = OperateContext.userInfo.COMPANY_ID.ToString() };
            List<SearchCondition> listSearch = new List<SearchCondition>();
            //listSearch.Add(condition);

            Expression<Func<vproduct, bool>> exp = SearchExpression.GetSearchExpression<vproduct>(vproduct, listSearch, SearchContains, "COMPANY_ID");

            var list = BLLSession.Current.vproductBLL.GetPagedList(page, rows, exp, sort, order);
            int count = DALSession.Current.vproductDAL.GetCount(exp);
            LayuiDataPager dataPager = new LayuiDataPager() { code = 0, count = count, data = list, msg = "", total = count, rows = list };

            return Json(dataPager, JsonRequestBehavior.AllowGet);
        }

        public ActionResult ProductEdit()
        {
            string action = Request.QueryString["action"].ToString();
            int Id = Commons.String2Int32(Request.QueryString["Id"]);
            string width = Request.QueryString["width"].ToString();
            product info = new product();
            if (action == "add")
            {
                Id = 0;
            }
            else
            {
                info = BLLSession.Current.productBLL.GetListBy(c => c.PRODUCT_ID == Id).FirstOrDefault();
            }
            var companylist = BLLSession.Current.companyBLL.GetListBy(c => 1==1).ToList();
            ViewBag.companylist = companylist;

            ViewData.Model = info;
            ViewData["action"] = action;
            ViewData["Id"] = Id.ToString();
            ViewData["width"] = width;
            return View();
        }

        public ActionResult GetProductInfoById(int Id)
        {
            var product = BLLSession.Current.vproductBLL.GetListBy(c => c.PRODUCT_ID == Id).FirstOrDefault();
            return Json(product, JsonRequestBehavior.AllowGet);
        }
        public ActionResult AddProduct(product info)
        {
            info.CREATED_BY = OperateContext.userInfo.NAME;
            info.CREATED_TIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            BLLSession.Current.productBLL.Add(info);
            return Json(new Message() { state = "success" });
        }

        public ActionResult EditProduct(product info)
        {
            info.CREATED_BY = OperateContext.userInfo.NAME;
            info.CREATED_TIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            BLLSession.Current.productBLL.Modify(info, "PRODUCT_NO", "DEVICE_ID", "PRODUCT_TYPE", "PRODUCT_NAME", "SOFTWARE_VERSION", "COMPANY_ID", "CREATED_TIME", "CREATED_BY");
            return Json(new Message() { state = "success" });
        }

        public ActionResult DeleteProductInfoById(int Id)
        {
            BLLSession.Current.productBLL.DelBy((d => d.PRODUCT_ID == Id));
            Message msg = new Message() { state = "success" };
            return Json(msg, JsonRequestBehavior.AllowGet);
        }

        #endregion

        #region=================================子产品==================================================
        public ActionResult SubProductListInfo()
        {
            return View();
        }
        public ActionResult SubProductDetail(int page, int rows, vsubproduct vsubproduct, string SearchContains, string sort, string order)
        {
            sort = string.IsNullOrEmpty(sort) ? "COMPANY_ID" : sort;
            order = string.IsNullOrEmpty(order) ? "asc" : order;

            //SearchCondition condition = new SearchCondition() { Field = "COMPANY_ID", Operator = "=", Value = OperateContext.userInfo.COMPANY_ID.ToString() };
            List<SearchCondition> listSearch = new List<SearchCondition>();
            //listSearch.Add(condition);

            Expression<Func<vsubproduct, bool>> exp = SearchExpression.GetSearchExpression<vsubproduct>(vsubproduct, listSearch, SearchContains, "COMPANY_ID");

            var list = BLLSession.Current.vsubproductBLL.GetPagedList(page, rows, exp, sort, order);
            int count = DALSession.Current.vsubproductDAL.GetCount(exp);
            LayuiDataPager dataPager = new LayuiDataPager() { code = 0, count = count, data = list, msg = "", total = count, rows = list };

            return Json(dataPager, JsonRequestBehavior.AllowGet);
        }

        public ActionResult SubProductEdit()
        {
            string action = Request.QueryString["action"].ToString();
            int Id = Commons.String2Int32(Request.QueryString["Id"]);
            string width = Request.QueryString["width"].ToString();
            vsubproduct info = new vsubproduct();
            if (action == "add")
            {
                Id = 0;
            }
            else
            {
                info = BLLSession.Current.vsubproductBLL.GetListBy(c => c.SUB_PRODUCT_ID == Id).FirstOrDefault();
            }
            var companylist = BLLSession.Current.companyBLL.GetListBy(c => 1 == 1).ToList();
            ViewBag.companylist = companylist;
            var productlist = BLLSession.Current.vproductBLL.GetListBy(c => 1 == 1).ToList();
            ViewBag.productlist = productlist;
            ViewData.Model = info;
            ViewData["action"] = action;
            ViewData["Id"] = Id.ToString();
            ViewData["width"] = width;
            return View();
        }

        public ActionResult GetSubProductInfoById(int Id)
        {
            var subproduct = BLLSession.Current.vsubproductBLL.GetListBy(c => c.SUB_PRODUCT_ID == Id).FirstOrDefault();
            return Json(subproduct, JsonRequestBehavior.AllowGet);
        }
        public ActionResult AddSubProduct(subproduct info)
        {
            var product = BLLSession.Current.productBLL.GetListBy(c => c.PRODUCT_ID == info.PRODUCT_ID).FirstOrDefault();
            info.PRODUCT_NO = product.PRODUCT_NO;
            info.COMPANY_ID = product.COMPANY_ID;
            info.CREATED_BY = OperateContext.userInfo.NAME;
            info.CREATED_TIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            BLLSession.Current.subproductBLL.Add(info);
            return Json(new Message() { state = "success" });
        }

        public ActionResult EditSubProduct(subproduct info)
        {
            var product = BLLSession.Current.productBLL.GetListBy(c => c.PRODUCT_ID == info.PRODUCT_ID).FirstOrDefault();
            info.PRODUCT_NO = product.PRODUCT_NO;
            info.COMPANY_ID = product.COMPANY_ID;
            info.CREATED_BY = OperateContext.userInfo.NAME;
            info.CREATED_TIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            BLLSession.Current.subproductBLL.Modify(info, "SUBPRODUCT_NO", "SUB_PRODUCT_NAME", "SUB_PRODUCT_TYPE", "COMPANY_ID", "PRODUCT_ID", "PRODUCT_NO", "CREATED_TIME", "CREATED_BY");
            return Json(new Message() { state = "success" });
        }

        public ActionResult DeleteSubProductInfoById(int Id)
        {
            BLLSession.Current.subproductBLL.DelBy((d => d.SUB_PRODUCT_ID == Id));
            Message msg = new Message() { state = "success" };
            return Json(msg, JsonRequestBehavior.AllowGet);
        }

        #endregion

    }
}
